//
//  AY_BookOpinionPage.m
//  AiYue
//
//  Created by boWen on 2023/11/13.
//

#import "AY_BookQiuBookPage.h"
#import "AY_BookOpinionCell1.h"
#import "AY_BookOpinionCell2.h"
#import "IQKeyboardManager.h"

@interface AY_BookQiuBookPage ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * baseTableView;
@property (nonatomic, strong) AY_BookOpinionCell1 * cell1;
@property (nonatomic, strong) ConfigModel *configModel;
@end

@implementation AY_BookQiuBookPage

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWithNav];
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [self getData];
}

- (void)getData {
    [AYApiRequest APPConfigSuccess:^(AY_ConfigModel * _Nonnull pkg) {
        self.configModel = pkg.data;
        [self.baseTableView reloadData];
    } failure:^(int status, NSString * _Nonnull info) {
    }];
}

#pragma mark 监听键盘的显示与隐藏
///键盘显示事件
- (void) keyboardWillShow:(NSNotification *)notification {
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        self.baseTableView.frame = CGRectMake(0, -50, SCREEN_W, SCREEN_H);
    }];
}

///键盘消失事件
- (void) keyboardWillHide:(NSNotification *)notification {
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        self.baseTableView.frame = CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, SCREEN_W, SCREEN_H);
    }];
}

- (void)setBookName:(NSString *)bookName {
    _bookName = bookName;
}

- (void)initWithNav {
    self.gk_navTitle = @"填写信息";
    [self.view addSubview:self.baseTableView];
    [self setupTopView];
    [self setupDownView];
}

- (UITableView *)baseTableView {
    if (!_baseTableView) {
        UITableView * myTableView = [AYTool creatMyTableView:CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, GK_SCREEN_WIDTH, GK_SCREEN_HEIGHT)];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.backgroundColor = [UIColor whiteColor];
        [myTableView registerClass:[AY_BookOpinionCell1 class] forCellReuseIdentifier:@"AY_BookOpinionCell1"];
        [myTableView registerClass:[AY_BookOpinionCell2 class] forCellReuseIdentifier:@"AY_BookOpinionCell2"];
        _baseTableView = myTableView;
    }
    return _baseTableView;
}

- (void)setupTopView {
    UIView * bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GK_SCREEN_WIDTH, YNUM(200))];
    UIImageView * topImage = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_top_qiushu.png") andBg:[UIColor clearColor] andRadius:YNUM(1) andAddSubView:bg];
    [topImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bg);
        make.centerY.equalTo(bg).offset(YNUM(20));
        make.width.offset(YNUM(153));
        make.height.offset(YNUM(103));
    }];
    
    UIView * bg_view1 = [AYTool createViewWithFrame:CGRectZero andBg:COLOR(@"#FBF2EF") andRadius:YNUM(6) andAddSubView:bg];
    [bg_view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bg).offset(YNUM(3));
        make.left.equalTo(bg);
        make.right.equalTo(bg);
        make.height.offset(YNUM(45));
    }];
    UILabel * tips = [AYTool createLabelWithFrame:CGRectZero andText:@"没有您找的书籍，我们表示抱歉，提交后我们尽快为您处理" andTextColor:COLOR(@"#FD8301") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(12)] andLines:1 andRadius:0 andAddSubView:bg_view1];
    [tips mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bg_view1).offset(YNUM(8));
        make.right.equalTo(bg_view1).offset(YNUM(-8));
        make.centerY.equalTo(bg_view1);

    }];
    
    self.baseTableView.tableHeaderView = bg;
}

- (void)setupDownView {
    UIView *footerV = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_H - YNUM(130), SCREEN_W, YNUM(130))];
    UIButton* sureBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"提交" andFont:[UIFont boldSystemFontOfSize:16] andTitleColor:[UIColor whiteColor] andBg:THEME_COLOR andImage:nil andBgImage:nil andTarget:self andAction:@selector(handleSubmitAction) andRadius:YNUM(8) andAddSubView:footerV];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(footerV).offset(YNUM(-30));
        make.left.equalTo(footerV).offset(YNUM(16));
        make.right.equalTo(footerV).offset(YNUM(-16));
        make.height.mas_offset(YNUM(45));
    }];
//    self.baseTableView.tableFooterView = footerV;
    [self.view addSubview:footerV];
}


#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0) {
        _cell1 = [tableView dequeueReusableCellWithIdentifier:@"AY_BookOpinionCell1"];
        _cell1.articleNameField.text = self.bookName;
        return _cell1;
    }else {
        AY_BookOpinionCell2 * cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookOpinionCell2"];
        cell.model = self.configModel;
        return cell;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 0 ? YNUM(320) : YNUM(130);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];
}

- (void)handleSubmitAction {
    if(kStringIsEmpty(_cell1.articleNameField.text))  {
        ShowToast(@"请先输入书籍名称");
        return;
    }
    if(kStringIsEmpty(_cell1.articleLeadOrAuthorField.text))  {
        ShowToast(@"请先输入作者主角名称");
        return;
    }
    if([self chineseCountOfString:_cell1.articleLeadOrAuthorField.text] < 2)  {
        ShowToast(@"作者主角名称字数大于或等于2个字");
        return;
    }
    [AYApiRequest postWithArticleDemand:_cell1.articleNameField.text articleLeadOrAuthor:_cell1.articleLeadOrAuthorField.text Success:^(AY_QiuBookModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            [self.navigationController popViewControllerAnimated:YES];
            ShowToast(@"提交成功");
        }else {
            ShowToast(pkg.msg);
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}

- (NSInteger)chineseCountOfString:(NSString *)string{
    int ChineseCount = 0;
    if (string.length == 0) {        return 0;
    }
    for (int i = 0; i<string.length; i++) {
    unichar c = [string characterAtIndex:i];
    if (c >=0x4E00 && c <=0x9FA5)        {
        ChineseCount++ ;//汉字
        }
    }
    return ChineseCount;
}
@end
